// index.js
Page({
  data: {
      newTask: '',
      todos: []
  },
  onLoad() {
    this.loadNotes();
  },
  
  loadNotes() {
    const todos = wx.getStorageSync('todos') || [];
    this.setData({ todos });
  },
  
  onPullDownRefresh() {
    this.loadNotes();
    wx.stopPullDownRefresh();
  },
  bindInput(e) {
      this.setData({
          newTask: e.detail.value
      });
  },
  addTodo() {
    if (this.data.newTask.trim() === '') {
      wx.showToast({
        title: '任务不能为空',
        icon: 'none'
      });
      return;
    }
    const newTodo = { title: this.data.newTask, completed: false };
    const todos = [...this.data.todos, newTodo];
    this.setData({
      todos,
      newTask: ''
    });
    wx.setStorageSync('todos', todos);
  },
  
  toggleTodo(e) {
    const index = e.currentTarget.dataset.index;
    const todos = this.data.todos.slice();
    todos[index].completed = !todos[index].completed;
    this.setData({ todos });
    wx.setStorageSync('todos', todos);
  },
  
  deleteTodo(e) {
    const index = e.currentTarget.dataset.index;
    wx.showModal({
      title: '删除提示',
      content: '确定要删除这个待办事项吗?',
      success: (res) => {
        if (res.confirm) {
          const todos = this.data.todos.filter((_, i) => i !== index);
          this.setData({ todos });
          wx.setStorageSync('todos', todos);
        }
      }
    });
  }
  
  
});